package com.aice.chinamapview.slice;

import com.aice.chinamapview.ResourceTable;
import com.noteam.chinamapview.ChinaMapView;
import com.noteam.chinamapview.HVScaleScrollView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import top.defaults.colorpicker.ColorPickerPopup;

import java.math.BigDecimal;

public class NarmalAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private ChinaMapView mapView;
    private DirectionalLayout rlMap;
    private HVScaleScrollView scaleScrollView;
    private int scale;
    int screenWidth;
    int mapWidth = 1450;
    private Button btn_province_color;
    private DirectionalLayout myLayout;
    private Button btn_border_unselect_color;
    private Button btn_border_select_color;
    private Button btn_province_name_color;
    private Checkbox checkbox_province_name;
    private Text provice_name;
    private Checkbox checkboxProvinceMoveScale;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_narmal);
        initView();
    }

    private void initView() {
        myLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_myLayout);
        mapView = (ChinaMapView) findComponentById(ResourceTable.Id_mapView);
        rlMap = (DirectionalLayout) findComponentById(ResourceTable.Id_rl_map);
        provice_name = (Text) findComponentById(ResourceTable.Id_provice_name);
        checkboxProvinceMoveScale = (Checkbox) findComponentById(ResourceTable.Id_checkbox_province_move_scale);
        checkboxProvinceMoveScale.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                drawCheckBox(component, canvas);
            }
        });
        checkbox_province_name = (Checkbox) findComponentById(ResourceTable.Id_checkbox_province_name);
        checkbox_province_name.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                drawCheckBox(component, canvas);
            }
        });
        checkbox_province_name.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                mapView.isShowCityName(isChecked);
                checkbox_province_name.invalidate();
            }
        });

        checkboxProvinceMoveScale.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                mapView.setOnlyClick(isChecked);
                checkboxProvinceMoveScale.invalidate();
            }
        });
        scaleScrollView = (HVScaleScrollView) findComponentById(ResourceTable.Id_scrollView);
        scaleScrollView.setChinaMapView(mapView);
        btn_province_color = (Button) findComponentById(ResourceTable.Id_btn_province_color);
        btn_province_color.setClickedListener(this::onClick);
        btn_border_unselect_color = (Button) findComponentById(ResourceTable.Id_btn_border_unselect_color);
        btn_border_unselect_color.setClickedListener(this::onClick);
        btn_border_select_color = (Button) findComponentById(ResourceTable.Id_btn_border_select_color);
        btn_border_select_color.setClickedListener(this::onClick);
        btn_province_name_color = (Button) findComponentById(ResourceTable.Id_btn_province_name_color);
        btn_province_name_color.setClickedListener(this::onClick);
        mapView.setOnProvinceSelectedListener(new ChinaMapView.OnProvinceSelectedListener() {
            @Override
            public void onProvinceSelected(ChinaMapView.Area pArea, boolean repeatClick) {
                /*if (repeatClick) {
                    scaleAndScroll();
                    return;
                }*/
                provice_name.setText(pArea.name);
            }
        });
        scale = (int)(screenWidth / mapWidth);
    }

    private void scaleAndScroll() {
        rlMap.setScaleX(scale);
        rlMap.setScaleY(scale);

        scaleScrollView.smoothScrollTo((rlMap.getWidth() - screenWidth) / 2, (int) (rlMap.getHeight() - (scale * mapWidth)) / 2 - 200);
        Intent resultIntent = new Intent();
        setResult(resultIntent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void drawCheckBox(Component component, Canvas canvas) {
        final int height = checkbox_province_name.getHeight();
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        BigDecimal divide = new BigDecimal(Float.intBitsToFloat(height)).divide(
            new BigDecimal(Float.intBitsToFloat(2)));
        float subtractValue = divide.subtract(
            new BigDecimal(Float.intBitsToFloat(8))).floatValue();
        canvas.drawCircle(subtractValue,
            divide.floatValue(), 20, paint);
        paint.setColor(new Color(Color.getIntColor("#FFFF4081")));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(6);
        canvas.drawCircle(subtractValue, divide.floatValue(), 20, paint);
        if (((Checkbox) component).isChecked()) {
            paint.setColor(new Color(Color.getIntColor("#FFFF4081")));
            paint.setStyle(Paint.Style.FILL_STYLE);
            canvas.drawCircle(subtractValue, divide.floatValue(), 12, paint);
        }

    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            // 修改省份的颜色
            case ResourceTable.Id_btn_province_color:
                new ColorPickerPopup.Builder(this)
                        .initialColor(Color.RED.getValue())
                        .enableBrightness(true)
                        .enableAlpha(true)
                        .okTitle("确定")
                        .cancelTitle("取消")
                        .showIndicator(true)
                        .showValue(true)
                        .setButtonClickListener(new ColorPickerPopup.ButtonClickListener() {
                            @Override
                            public void okClick(int color) {
                                mapView.setProvinceColor(color);
                            }
                            @Override
                            public void cancelClick() {
                            }
                        })
                        .build()
                        .show(myLayout, new ColorPickerPopup.ColorPickerObserver() {
                            @Override
                            public void onColorPicked(int color) {
                                System.out.println("color=================" + color);
                            }
                        });
                break;
            // 修改未选中省份的边框颜色
            case ResourceTable.Id_btn_border_unselect_color:
                new ColorPickerPopup.Builder(this)
                        .initialColor(Color.RED.getValue())
                        .enableBrightness(true)
                        .enableAlpha(true)
                        .okTitle("确定")
                        .cancelTitle("取消")
                        .showIndicator(true)
                        .showValue(true)
                        .setButtonClickListener(new ColorPickerPopup.ButtonClickListener() {
                            @Override
                            public void okClick(int color) {
                                mapView.setUnSelectBorderColor(color);
                            }
                            @Override
                            public void cancelClick() {
                            }
                        })
                        .build()
                        .show(myLayout, new ColorPickerPopup.ColorPickerObserver() {
                            @Override
                            public void onColorPicked(int color) {

                            }
                        });
                break;
                //修改选中省份的边框颜色
            case ResourceTable.Id_btn_border_select_color:
                new ColorPickerPopup.Builder(this)
                        .initialColor(Color.RED.getValue())
                        .enableBrightness(true)
                        .enableAlpha(true)
                        .okTitle("确定")
                        .cancelTitle("取消")
                        .showIndicator(true)
                        .showValue(true)
                        .setButtonClickListener(new ColorPickerPopup.ButtonClickListener() {
                            @Override
                            public void okClick(int color) {
                                mapView.setSelectBorderColor(color);
                            }
                            @Override
                            public void cancelClick() {
                            }
                        })
                        .build()
                        .show(myLayout, new ColorPickerPopup.ColorPickerObserver() {
                            @Override
                            public void onColorPicked(int color) {
                                System.out.println("");
                            }
                        });
                break;
            case ResourceTable.Id_btn_province_name_color:
                new ColorPickerPopup.Builder(this)
                        .initialColor(Color.RED.getValue())
                        .enableBrightness(true)
                        .enableAlpha(true)
                        .okTitle("确定")
                        .cancelTitle("取消")
                        .showIndicator(true)
                        .showValue(true)
                        .setButtonClickListener(new ColorPickerPopup.ButtonClickListener() {
                            @Override
                            public void okClick(int color) {
                                mapView.setProvinceNameColor(color);
                            }
                            @Override
                            public void cancelClick() {
                            }
                        })
                        .build()
                        .show(myLayout, new ColorPickerPopup.ColorPickerObserver() {
                            @Override
                            public void onColorPicked(int color) {
                                System.out.println("color=================" + color);
                            }
                        });
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + component.getId());
        }
    }
}
